import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import { clearLoginInfo } from '@/utils'
import  commonVar  from '@/assets/commonVar.vue'
Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const
  _import = require('./import-' + process.env.NODE_ENV)

const globalRoutes = [
  { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } },
  { path: '/register', component: _import('common/register'), name: 'register', meta: { title: '注册' } }
]

const mainRoutes = {
  mode: 'history',
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '主入口整体布局' },
  children: [
    // 通过meta对象设置路由展示方式
    // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
    // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
    // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
    { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页', keepAlive: false}},
    { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题', keepAlive: false } },
    { path: '/user', component: _import('common/user'), name: 'user', meta: {title: '用户', isTab: true, keepAlive: false}},
    { path: '/file', component: _import('common/file'), name: 'file', meta: {title: '文件', isTab: true, keepAlive: true}},
    { path: '/fileInfo', component: _import('common/fileInfo'), name: 'fileInfo', meta: {title: '文件信息', keepAlive: false}},
    { path: '/diskInfo', component: _import('common/diskInfo'), name: 'diskInfo', meta: {title: '磁盘信息', isTab: true, keepAlive: false}},
    { path: '/fileContent', component: _import('common/fileContent'), name: 'fileContent', meta: {title: '文件内容', isTab: true, keepAlive: false}},

  ],
  beforeEnter (to, from, next) {
    if(to.name === 'register'){
      next();
    }
    let token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next({ name: 'login' })
    }
    next();
  }
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: globalRoutes.concat(mainRoutes)
  /*routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]*/
})

export default router
